package com.cskaoyan.servlet;

import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.service.ConfigService;
import com.cskaoyan.service.ConfigServiceImpl;
import com.cskaoyan.util.JacksonUtil;
import com.cskaoyan.util.ResponseUtil;
import com.cskaoyan.util.URIUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author stone
 * @date 2023/03/20 15:19
 */
@WebServlet("/admin/config/*")
public class AdminConfigServlet extends HttpServlet {
    private ConfigService systemConfigService = new ConfigServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = URIUtil.getOperation(this.getClass(), req);
        switch (operation) {
            case "/mall":
                mall(req, resp);
                break;
            case "/express":
                express(req, resp);
                break;
            case "/order":
                order(req, resp);
                break;
            case "/wx":
                wx(req, resp);
                break;
        }
    }

    private void wx(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            //GET方法做查询
            String[] strings = {"market_wx_catlog_goods",
                    "market_wx_catlog_list",
                    "market_wx_index_brand",
                    "market_wx_index_hot",
                    "market_wx_index_new",
                    "market_wx_index_topic",
                    "market_wx_share"};
            Map<String, String> map = QueryConfig(strings);
            ResponseUtil.responseJson(response, BaseRespVo.ok(map));
        }else {
            //POST方法做更新
            updateSystemConfigByPost(request,response);
        }
    }

    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            //GET方法做查询
            String[] strings = {"market_order_comment","market_order_unconfirm","market_order_unpaid"};
            Map<String, String> map = QueryConfig(strings);
            ResponseUtil.responseJson(response, BaseRespVo.ok(map));
        }else {
            //POST方法做更新
            updateSystemConfigByPost(request,response);
        }
    }

    private void express(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            //GET方法做查询
            String[] strings = {"market_express_freight_min", "market_express_freight_value"};
            Map<String, String> map = QueryConfig(strings);
            ResponseUtil.responseJson(response, BaseRespVo.ok(map));

        }else {
            //POST方法做更新
            updateSystemConfigByPost(request,response);

        }
    }

    private Map<String, String> QueryConfig(String[] strArray) {
        Map<String, String> configMap = systemConfigService.QueryConfig(strArray);
        return configMap;
    }

    private void updateSystemConfigByPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String s = request.getReader().readLine();
        Map<String,String> dataMap = JacksonUtil.read(s, Map.class);
        int i = systemConfigService.updateConfig(dataMap);
        if (i == dataMap.size()) {
            ResponseUtil.responseJson(response,BaseRespVo.ok(dataMap));
        }else {
            ResponseUtil.responseJson(response,BaseRespVo.fail("更新失败"));
        }
    }

    private void mall(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getMethod();
        if ("GET".equals(method)) {
            //GET方法做查询
            mallQuery(request, response);
        }else {
            //POST方法做更新
            mallModify(request, response);
        }
    }

    private void mallModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonStr = request.getReader().readLine();
        Map<String, String> data = JacksonUtil.read(jsonStr, Map.class);
        systemConfigService.updateConfig(data);
        ResponseUtil.responseOkJsonVo(response,null);
    }

    private void mallQuery(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Map<String, String> data = systemConfigService.listMall();
        ResponseUtil.responseOkJsonVo(response,data);
    }
}
